package ytdp.careagle.com.ytdp.mvp.presenter;


import android.content.Context;
import android.text.TextUtils;

import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.entity.SmsCodeIdEntity;
import ytdp.careagle.com.ytdp.mvp.mode.LoginModel;
import ytdp.careagle.com.ytdp.mvp.view.IForgetPasswordView;
import ytdp.careagle.com.ytdp.net.MyObserver;
import ytdp.careagle.com.ytdp.net.Result;
import ytdp.careagle.com.ytdp.utils.MobileCheckUtils;

/**
 * Created by admin on 2017/9/12.
 */

public class ForgetPasswordPresenter {
    private IForgetPasswordView view;
    private LoginModel model;

    public ForgetPasswordPresenter(IForgetPasswordView view) {
        this.view = view;
        model = new LoginModel();
    }

    public void resetPassword(Context context) {
        String mobileNumber = view.getMobileNumber();
        String newPassword = view.getNewPassword();
        String code = view.getCode();
        if (TextUtils.isEmpty(mobileNumber)) {
            view.showMessage("手机号不能为空");
        } else if (!MobileCheckUtils.isMobileNO(mobileNumber)) {
            view.showMessage("输入正确的手机号");
        } else {
            view.showProgress("正在修改密码");
            model.resetPassword(context, code, newPassword, mobileNumber, new MyObserver<Result>() {
                @Override
                public void onNext(Result result) {
                    super.onNext(result);
                    if (result.getCode() == G.REQUEST_OK) {
                        view.resetPasswordSuccess();
                    } else {
                        view.showMessage(result.getMessage());
                    }
                    view.hideProgress();
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    view.showMessage("访问不到服务器");
                    view.hideProgress();
                }
            });
        }
    }

    public void getVerificationCode(Context context) {
        String mobileNumber = view.getMobileNumber();
        if (TextUtils.isEmpty(mobileNumber)) {
            view.showMessage("手机号不能为空");
        } else if (!MobileCheckUtils.isMobileNO(mobileNumber)) {
            view.showMessage("输入正确的手机号");
        } else {
            view.showProgress("正在获取验证码");
            model.getVerificationCode(context, mobileNumber, new MyObserver<Result<SmsCodeIdEntity>>() {
                @Override
                public void onNext(Result<SmsCodeIdEntity> smsCodeIdEntityResult) {
                    super.onNext(smsCodeIdEntityResult);
                    if (smsCodeIdEntityResult.getCode() == G.REQUEST_OK) {
                        view.startTime();
                    } else {
                        view.showMessage(smsCodeIdEntityResult.getMessage());
                    }
                    view.hideProgress();
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    view.showMessage("获取验证码失败");
                    view.hideProgress();
                }
            });
        }
    }
}
